﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBatisNet.DataMapper;
namespace DAL
{

    /// <summary>
    /// 数据库操作基类
    /// </summary>
   public class BaseDB
    {
       /// <summary>
       /// 插入一条数据
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="statementName"></param>
       /// <param name="t"></param>
       /// <returns></returns>
        public static int Insert<T>(string statementName,T t) {
            ISqlMapper iSqlMapper = Mapper.Instance();
           
            if(iSqlMapper!=null){
                return (int)iSqlMapper.Insert(statementName,t);
            }
            return 0;
        }
        /// <summary>
        /// 根据主键更新一条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementName"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int Update<T>(string statementName, T t)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return (int)iSqlMapper.Update(statementName, t);
            }
            return 0;
        }
       /// <summary>
       /// 根据主键删除一条记录
       /// </summary>
       /// <param name="statementName"></param>
       /// <param name="primaryKeyId"></param>
       /// <returns></returns>
        public static int Delete(string statementName, int primaryKeyId)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return (int)iSqlMapper.Delete(statementName, primaryKeyId);
            }
            return 0;
        }
       /// <summary>
       /// 根据主键获得对象信息
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="statementName"></param>
       /// <param name="primaryKeyId"></param>
       /// <returns></returns>
        public static T Get<T>(string statementName, int primaryKeyId) where T:class
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return iSqlMapper.QueryForObject<T>(statementName, primaryKeyId);
            }
            return null;
        }
       /// <summary>
       /// 查询全部数据
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="statementName">实体类名</param>
       /// <param name="parameterObject"></param>
       /// <returns></returns>
        public static IList<T> QueryForList<T>(string statementName, object parameterObject = null)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return iSqlMapper.QueryForList<T>(statementName, parameterObject);
            }
            return null;
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementName">实体类名</param>
        /// <param name="skipResult">跳过多少行</param>
        /// <param name="maxsize">最大行数</param>
        /// <param name="parameterObject"></param>
        /// <returns></returns>
        public static IList<T> QueryForListSkip<T>(string statementName,int skipResult,int maxsize, object parameterObject = null)
        {
            ISqlMapper iSqlMapper = Mapper.Instance();
            if (iSqlMapper != null)
            {
                return iSqlMapper.QueryForList<T>(statementName, parameterObject, skipResult, maxsize);
            }
            return null;
        }


    }
}
